<!-- 头部信息栏 -->
<template>
  <div class="header_container">
    <!-- 标题栏 -->
    <div class="header_title">
      <div class="header_logo">
        <img class="logo_img" src="@/assets/images/logo.jpg" alt="" />
      </div>
      <div class="header_title_text">XXXXXXXXXXXXXX系统</div>
    </div>

    <!-- 菜单栏 -->
    <div class="header_menu">
      <el-menu :default-active="activeMenuItem" router mode="horizontal" background-color="#3169d7" text-color="#fff"
        active-text-color="#fff" @select="handleSelect">
        <el-menu-item v-for="item in routerList" :index="item.path" :key="item.path">{{ item.name }}</el-menu-item>
      </el-menu>
    </div>

    <!-- 退出按钮 -->
    <div class="header_logout">
      <el-button type="text" @click="logout">退出</el-button>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router'

//定义emits事件
const emits = defineEmits(['select']);

const router = useRouter();
const routerList = ref([]);//一级路由列表，用于生成一级菜单
const secondRouterList = ref([]);//二级路由列表，用于生成二级菜单
const activeMenuItem = ref('/QualityAssessment');//一级菜单默认选中项
const selectedMenuItemName = ref({});//一级菜单选中项名称

onMounted(() => {
  // 获取所有路由
  routerList.value = router.getRoutes()[0].children;
  //初始化时，默认设置二级菜单数据
  emits('select', routerList.value[0].children);
})

const handleSelect = (key) => {
  //根据key值获取路由名称
  selectedMenuItemName.value = routerList.value.find(item => item.path === key).name;
  //设置一级菜单选中项
  activeMenuItem.value = key;
  //获取当前一级路由下的所有子路由用于生成二级子菜单
  routerList.value.forEach(item => {
    if (item.path === key) {
      secondRouterList.value = item.children;
    }
  });
  //触发父组件事件,将二级路由列表和当前一级路由名称传递给父组件
  emits('select', secondRouterList.value, selectedMenuItemName.value);
}
const logout = () => {
  //跳转登录页
  router.push('/login');
  //清除token
  localStorage.removeItem('token');
}
</script>

<style lang="less" scoped>
.header_container {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: row;

  .header_title {
    width: 33%;
    display: flex;
    flex-direction: row;
    align-items: center;

    .header_logo {
      width: 60px;
      height: 60px;
      margin-left: 10px;
      display: flex;
      align-items: center;

      .logo_img {
        height: 50px;
        width: 50px;
      }
    }

    .header_title_text {
      margin-left: 10px;
      color: #f5f5f5;
      font-size: 24px;
      font-weight: bold;
    }
  }

  .header_menu {
    width: 76%;
  }

  .header_logout {
    width: 4%;
  }
}
</style>

<style lang="less" scoped>
.el-menu--horizontal.el-menu {
  border-bottom: 0px;
}

.el-menu--horizontal>.el-menu-item {
  color: #fff;
  font-size: 16px;
  font-weight: bolder;
}

.el-menu--horizontal .el-menu-item:not(.is-disabled):focus,
.el-menu--horizontal .el-menu-item:not(.is-disabled):hover {
  color: #fff;
  outline: none;
}
</style>
